<?php


namespace app\system\service;

class CustomStatisticsService
{
	public static function getlist($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%" . $options["name"] . "%"];
		}
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		$count = \think\Db::name("app_custom_statistics_list")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("app_custom_statistics_list")->where($where)->page($options["page"], $options["limit"])->order("id", "asc")->select();
		$data = [];
		foreach ($list as $item) {
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("app_custom_statistics_list")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "ID不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => $item];
	}
	public static function add($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_custom_statistics_list")->where(["appid" => $options["appid"], "name" => $options["name"]])->find();
			if (!empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "该名称已存在"];
			}
			$data = ["appid" => $options["appid"], "name" => $options["name"], "describe" => $options["describe"] ?? "", "addtime" => time()];
			\think\Db::name("app_custom_statistics_list")->insert($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_custom_statistics_list")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$row = \think\Db::name("app_custom_statistics_list")->where(["appid" => $options["appid"], "name" => $options["name"]])->find();
			if (!empty($row) && $row["id"] != $options["id"]) {
				return ["code" => \app\system\model\Code::fail, "msg" => "该名称已存在"];
			}
			$data = ["name" => $options["name"], "describe" => $options["describe"] ?? ""];
			\think\Db::name("app_custom_statistics_list")->where(["id" => $options["id"], "appid" => $options["appid"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_custom_statistics_list")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_custom_statistics_list")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_data($options) : array
	{
		$row = \think\Db::name("app_custom_statistics_list")->where(["id" => $options["id"]])->find();
		if (empty($row)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$today_Ymd = date("Ymd");
		$yesterday_Ymd = date("Ymd", strtotime("-1 day"));
		$hour24 = ["dates" => DataService::getHourDate(false), "names" => DataService::getHourDate(true), "datas" => DataService::getHourData($options["appid"], \time(), \app\common\model\AppData::AppCustomStatistics, $options["id"])];
		$day7 = ["dates" => DataService::getDayDate(time(), 7, false), "names" => DataService::getDayDate(time(), 7, true), "datas" => DataService::getDayData($options["appid"], \time(), 7, \app\common\model\AppData::AppCustomStatistics, $options["id"])];
		$statistics = ["today" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "Ymd", $today_Ymd, 0, $options["id"]), "yesterday" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "Ymd", $yesterday_Ymd, 0, $options["id"]), "this_week" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "YW", date("YW"), 0, $options["id"]), "last_week" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "YW", date("YW", strtotime("-1 week")), 0, $options["id"]), "this_month" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "Ym", date("Ym"), 0, $options["id"]), "last_month" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "Ym", date("Ym", strtotime("-1 month")), 0, $options["id"]), "this_year" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "Y", date("Y"), 0, $options["id"]), "last_year" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AppCustomStatistics, "Y", date("Y", strtotime("-1 year")), 0, $options["id"])];
		$data["chart"] = ["hour" => $hour24 ?? [], "day" => $day7 ?? []];
		$data["statistics"] = $statistics ?? [];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
}